﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoffeeTeaTrading.Repository.Models;
using CoffeeTeaTrading.UI.Utils;

namespace CoffeeTeaTrading.UI.Controllers
{
    public class ProductController : GenericController
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View(DBContext.Instance.ProductSet);
        }

        //
        // GET: /Product/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Product/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Product/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "Id")]Product product, int? ProdTypeId)
        {
            var prodType = ProdType.GetByPK(ProdTypeId != null ? ProdTypeId.Value : 0);
            product.ProdType = prodType;
            product.Type = prodType.Name;

            if (product.CheckBeforeSave())
            {
                DBContext.Instance.AddToProductSet(product);
                DBContext.Instance.SaveChanges();

                return RedirectToAction("Index");
            }
            else
            {
                AddViolataionsToModelState(product.GetRuleViolations());
            }

            return View(product);
        }
        
        //
        // GET: /Product/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Product/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Product/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Product/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
